package com.ksyun.ks3.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.SimpleTimeZone;

import android.net.ParseException;

import com.ksyun.ks3.exception.Ks3ClientException;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

public class DateUtil {

	public static enum DATETIME_PROTOCOL {
		RFC1123, ISO8861;
	};
	public static Date parseRfc822Date(String dateString) throws java.text.ParseException {
		return getRfc822DateFormat().parse(dateString);
	}

	private static DateFormat getRfc822DateFormat() {
		SimpleDateFormat rfc822DateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
		rfc822DateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
		return rfc822DateFormat;
	}
	/**
	 * 获取指定时间对应的毫秒数
	 * @param time "HH:mm:ss"
	 * @return
	 */
	public static long getTimeMillis(String time) {
		try {
			DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
			DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
			Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);
			return curDate.getTime();
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	public static String convertDate2Str(Date date, DATETIME_PROTOCOL protocol) {
		if (protocol.equals(DATETIME_PROTOCOL.RFC1123)) {

			org.joda.time.format.DateTimeFormatter RFC1123_DATE_TIME_FORMATTER = DateTimeFormat
					.forPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'")
					.withZoneUTC().withLocale(Locale.ENGLISH);

			return RFC1123_DATE_TIME_FORMATTER.print(date.getTime());

		} else if (protocol.equals(DATETIME_PROTOCOL.ISO8861)) {

			DateTimeFormatter ISO8861_FORMATTER = ISODateTimeFormat.dateTime()
					.withZoneUTC();
			return ISO8861_FORMATTER.print(date.getTime());
		}
		return null;
	}
	// date to string
	public static String ConverToString(Date date) {
		SimpleDateFormat df = new SimpleDateFormat(
				"EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH);

		return df.format(date);
	}

	// string to date
	public static Date ConverToDate(String strDate) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
		sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
		if (strDate.contains("-") && strDate.contains("T")) {
			if (strDate.endsWith("Z")) {
				strDate = strDate.replace("Z", " GMT");
				sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS z", Locale.UK);
			} else if(strDate.endsWith("+08:00")){
				strDate = strDate.replace("+08:00", " GMT");
				sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS z", Locale.UK);
			}
		}
		try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			throw new Ks3ClientException(
						"The server did not return the expected value,it is "
								+ strDate, e);
		}
	}
	public static String GetUTCTime() {
		SimpleDateFormat df = new SimpleDateFormat(
				"EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH);
		
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		return df.format(cal.getTime());
	}
}
